//
//  BVPlaygroundDetailView.m
//  AFNetworking
//
//  Created by xiute on 2019/10/22.
//

#import "BVPlaygroundDetailView.h"
#import "BVPlaygroundDetailImageCollectionViewCell.h"
#import "BVGuideCollectionViewCell.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "BVLocationUtil.h"


typedef NS_ENUM(NSUInteger, BVGuideMapType) {
    BVGuideMapTypeBaidu,
    BVGuideMapTypeAmap,
    BVGuideMapTypeTencentMap,
    BVGuideMapTypeSogoMap
};

@interface BVPlaygroundDetailView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *imageCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionView *guidCollectionView;

@property (nonatomic, strong) NSMutableArray *guideDataSource;

@property (nonatomic, strong) BVMetaArena *arena;

@end

@implementation BVPlaygroundDetailView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.backButton setContentEdgeInsets:UIEdgeInsetsMake(11, 14, 11, 14)];
    self.imageCollectionView.delegate = self;
    self.imageCollectionView.dataSource = self;
    self.guidCollectionView.delegate = self;
    self.guidCollectionView.dataSource = self;
    [self.imageCollectionView registerNib:[UINib nibWithNibName:@"BVPlaygroundDetailImageCollectionViewCell" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_HOME_SCENE_MODULE]] forCellWithReuseIdentifier:@"BVPlaygroundDetailImageCollectionViewCell"];
    [self.guidCollectionView registerNib:[UINib nibWithNibName:@"BVGuideCollectionViewCell" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_HOME_SCENE_MODULE]] forCellWithReuseIdentifier:@"BVGuideCollectionViewCell"];
    self.imageCollectionView.showsHorizontalScrollIndicator = NO;
    self.guidCollectionView.showsHorizontalScrollIndicator = NO;
}

- (IBAction)backAct:(id)sender {
    if (self.backBlock) {
        self.backBlock();
    }
    [self removeFromSuperview];
}

- (void)configWithModel:(BVMetaArena *)model {
    self.nameLabel.text = model.name;
    self.addressLabel.text = model.address;
    [self.guidCollectionView reloadData];
    [self.imageCollectionView reloadData];
    self.arena = model;
    
}

- (IBAction)goAct:(id)sender {
    __block NSDictionary *dic = nil;
    [self.guideDataSource enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj[@"select"] boolValue]) {
            dic = obj;
            *stop = YES;
        }
    }];
    
    [[BVLocationUtil sharedInstance] startLocationWithBlock:^(CLLocation * _Nonnull location) {
        [self backAct:nil];
        switch ([dic[@"type"] integerValue]) {
            case BVGuideMapTypeBaidu: {
                NSString *str = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%@,%@|name:%@&mode=driving", location.coordinate.latitude, location.coordinate.longitude,  self.arena.location.latitude, self.arena.location.longitude, self.arena.name];
                NSLog(@"%@", str);
                str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                if (!success) {
                    [SVProgressHUD showErrorWithStatus:@"未安装百度地图"];
                }
            }
                break;
            case BVGuideMapTypeSogoMap: {
                NSString *str = [NSString stringWithFormat:@"baidumap://map/direction?destination=%@,%@&coord_type=gcj02&mode=driving&src=ios.baidu.openAPIdemo", self.arena.location.latitude, self.arena.location.longitude];
                str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                if (!success) {
                    [SVProgressHUD showErrorWithStatus:@"未安装搜狗地图"];
                }
            }
                break;
            case BVGuideMapTypeTencentMap: {
                NSString *str = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f, %f&tocoord=%@,%@&coord_type=1&policy=0&refer=%@", location.coordinate.latitude, location.coordinate.longitude, self.arena.location.latitude, self.arena.location.longitude, @"app name"];
                str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                if (!success) {
                    [SVProgressHUD showErrorWithStatus:@"未安装腾讯地图"];
                }
            }
                break;
            case BVGuideMapTypeAmap: {
                NSString *str = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%@&lon=%@&dev=1&style=2", @"app name", @"bvbasketball", self.arena.name, self.arena.location.latitude, self.arena.location.longitude];
                str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                if (!success) {
                    [SVProgressHUD showErrorWithStatus:@"未安装高德地图"];
                }
            }
                break;
                
            default:
                break;
        }
        
    }];
    
}

#pragma mark - collectionview delegate and datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.imageCollectionView) {
        return 3;
    }
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.imageCollectionView) {
        return CGSizeMake((194.0/108.0) * 54, 54);
    }
    return CGSizeMake(53, 65);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.imageCollectionView) {
        BVPlaygroundDetailImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BVPlaygroundDetailImageCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }
    BVGuideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BVGuideCollectionViewCell" forIndexPath:indexPath];
    [cell configWithModel:self.guideDataSource[indexPath.row]];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (collectionView == self.guidCollectionView) {
        [self.guideDataSource enumerateObjectsUsingBlock:^(NSMutableDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj setValue:@(NO) forKey:@"select"];
        }];
        NSMutableDictionary *dic = self.guideDataSource[indexPath.row];
        [dic setValue:@(YES) forKey:@"select"];
        [self.guidCollectionView reloadData];
    }
}
\

- (NSMutableArray *)guideDataSource {
    if (!_guideDataSource) {
        _guideDataSource = @[@{@"image": @"map_pic_baidu", @"name": @"百度地图", @"type": @(BVGuideMapTypeBaidu)}.mutableCopy,
                             @{@"image": @"map_pic_amap", @"name": @"高德地图", @"type": @(BVGuideMapTypeAmap)}.mutableCopy,
                             @{@"image": @"map_pic_tenxun", @"name": @"腾讯地图", @"type": @(BVGuideMapTypeTencentMap)}.mutableCopy,
                             @{@"image": @"map_pic_sogo", @"name": @"搜狗地图", @"type": @(BVGuideMapTypeSogoMap)}.mutableCopy].mutableCopy;
    }
    return _guideDataSource;
}

@end
